package com.vhly.remoteapi.yahoo.contact;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-5
 * Time: 16:35:13
 * Email: vhly@163.com
 */
public abstract class ContactObject {

    /**
     * Contact id
     */
    public String id;
    /**
     * Contact uri
     */
    public String uri;
    /**
     * Contact crated time
     */
    public String created;
    /**
     * Contact updated time
     */
    public String updated;
    /**
     * Contact status
     */
    public Status status;

    /**
     * Read object from ObjectInput
     *
     * @param din ObjectInput
     */
    protected void readObject(DataInput din) throws IOException {
        id = din.readUTF();
        uri = din.readUTF();
        created = din.readUTF();
        updated = din.readUTF();
        String s = din.readUTF();
        if (s.equals("add")) {
            status = Status.ADD;
        } else if (s.equals("update")) {
            status = Status.UPDATE;
        } else if (s.equals("remove")) {
            status = Status.REMOVE;
        }
    }

    /**
     * Write object to ObjectOutput
     *
     * @param dout Output
     */
    protected void writeObject(DataOutput dout) throws IOException {
        dout.writeUTF(id);
        dout.writeUTF(uri);
        dout.writeUTF(created);
        dout.writeUTF(updated);
        dout.writeUTF(status.toString());
    }

    /**
     * Parse DOM element
     *
     * @param item Element
     */
    public void parse(Element item) {
        if (item != null) {
            NodeList lst = item.getElementsByTagName("id");
            if (lst != null) {
                int len = lst.getLength();
                if (len > 0) {
                    Element el = (Element) lst.item(0);
                    NodeList cs = el.getChildNodes();
                    if (cs != null && cs.getLength() > 0) {
                        Node nd = cs.item(0);
                        if (nd instanceof Text) {
                            Text txt = (Text) nd;
                            id = txt.getNodeValue();
                        }
                    }
                }
            }

            String att = item.getAttribute("yahoo:uri");
            if (att != null) {
                uri = att;
            }

            att = item.getAttribute("yahoo:created");
            if (att != null) {
                created = att;
            }

            att = item.getAttribute("yahoo:updated");
            if (att != null) {
                updated = att;
            }

            lst = item.getElementsByTagName("op");
            if (lst != null) {
                int len = lst.getLength();
                if (len > 0) {
                    Element el = (Element) lst.item(0);
                    NodeList cs = el.getChildNodes();
                    if (cs != null && cs.getLength() > 0) {
                        Node nd = cs.item(0);
                        if (nd instanceof Text) {
                            Text txt = (Text) nd;
                            String op = txt.getNodeValue();
                            if (op != null) {
                                if (op.equals("add")) {
                                    status = Status.ADD;
                                } else if (op.equals("update")) {
                                    status = Status.UPDATE;
                                } else if (op.equals("remove")) {
                                    status = Status.REMOVE;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
